//
//  YDOAApplyDetialVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/6/2.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit
import HandyJSON



var data : Array = [
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":"",
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":"",
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":"",
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":"",
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":"",
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":"",
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":"",
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":"",
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":"",
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":0,
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":0,
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":0,
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":0,
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":0,
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":0,
     ],
    ["id" : 1,
     "num":1,
     "pic":"http://img0.imgtn.bdimg.com/it/u=4195805644,827754888&fm=26&gp=0.jpg",
     "name":"黑色签字笔",
     "count":10000,
     "applyNum":0,
     ],
]






class YDOAApplyDetialVC: UIViewController {

    @IBOutlet weak var myTableView: UITableView!
    
    
    lazy var selectData : Array = Array<ApplyModel>()//选择物品的数据
    lazy var dataSource : Array = Array<ApplyModel>()//可申请物品的数据
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "物资申请详情"
        self.automaticallyAdjustsScrollViewInsets = false
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        myTableView.register( UINib(nibName: "YDOAApplyDetialCell", bundle: nil), forCellReuseIdentifier: "YDOAApplyDetialCell")
        for i in 0..<data.count {
            
            guard let dict = data[i] as? NSDictionary else { return  }
            
            guard let model = JSONDeserializer<ApplyModel>.deserializeFrom(dict:dict) else { return  }
            self.dataSource.append(model)

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    ///确定按钮点击
    @IBAction func sureBtnClick(_ sender: Any) {
        
        let  vc = YDOAApplyListViewController()
        vc.dataSoure = self.selectData
        navigationController?.pushViewController(vc, animated: true)
        
        
        
        
    }

   
}


extension YDOAApplyDetialVC : UITableViewDelegate,UITableViewDataSource {

    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        
        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 40))
        let lab = UILabel.ym_lable(text: "编号", fontSize: 15, color: UIColor.black)
        lab.frame = CGRect(x: 0, y: 0, width: ScreenWidth / 5, height: 40)
        lab.textAlignment = .center
        lab.isUserInteractionEnabled = true
        view1.addSubview(lab)
        let  lab1 = UILabel.ym_lable(text: "图片", fontSize: 15, color: UIColor.black)
        lab1.frame = CGRect(x: ScreenWidth / 5, y: 0, width: ScreenWidth / 5, height: 40)
        lab1.textAlignment = .center
        lab1.isUserInteractionEnabled = true
        view1.addSubview(lab1)
        let  lab2 = UILabel.ym_lable(text: "名称", fontSize: 15, color: UIColor.black)
        lab2.frame = CGRect(x: (ScreenWidth / 5) * 2, y: 0, width: ScreenWidth / 5, height: 40)
        lab2.textAlignment = .center
        lab2.isUserInteractionEnabled = true
        view1.addSubview(lab2)
        
        let  lab3 = UILabel.ym_lable(text: "仓库数量", fontSize: 15, color: UIColor.black)
        lab3.frame = CGRect(x: (ScreenWidth / 5) * 3, y: 0, width: ScreenWidth / 5, height: 40)
        lab3.textAlignment = .center
        lab3.isUserInteractionEnabled = true
        view1.addSubview(lab3)
        
        
        let  lab4 = UILabel.ym_lable(text: "申请数量", fontSize: 15, color: UIColor.black)
        lab4.frame = CGRect(x: (ScreenWidth / 5) * 4, y: 0, width: ScreenWidth / 5, height: 40)
        lab4.textAlignment = .center
        lab4.adjustsFontSizeToFitWidth = true
        lab4.isUserInteractionEnabled = true
        view1.addSubview(lab4)
        
        view1.tag = 500 + section
       
        let  lineView = UIView(frame: CGRect(x: 0, y: 39, width: ScreenWidth, height: 1))
        lineView.backgroundColor = UIColor.ym_colorWithRGB(r: 222, g: 222, b: 222)
        view1.addSubview(lineView)
        
        view1.backgroundColor = UIColor.lightGray
        return view1
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell  = tableView.dequeueReusableCell(withIdentifier: "YDOAApplyDetialCell") as? YDOAApplyDetialCell else {
            return YDOAApplyDetialCell()
        }
        cell.selectionStyle = .none
        cell.model = self.dataSource[indexPath.row]
        cell.callback = {(model : ApplyModel) in
            self.selectData.append(model)
            print("目前的数据哟=======\(self.selectData)")
        }
        return  cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    
}
